package com.comprehensive.dataservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comprehensive.common.dto.PageResponse;
import com.comprehensive.dataservice.common.dtoConverter.PageConverter;
import com.comprehensive.dataservice.entity.MovieEpisodes;
import com.comprehensive.dataservice.entity.MovieSeasons;
import com.comprehensive.dataservice.entity.Movies;
import com.comprehensive.dataservice.service.MovieEpisodesService;
import com.comprehensive.dataservice.service.MovieSeasonsService;
import com.comprehensive.dataservice.service.MoviesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/movie")
public class MovieController {

    private final MoviesService moviesService;
    private final MovieSeasonsService movieSeasonsService;
    private final MovieEpisodesService movieEpisodesService;
    @Autowired
    public MovieController(MoviesService moviesService, MovieSeasonsService movieSeasonsService, MovieEpisodesService movieEpisodesService) {
        this.moviesService = moviesService;
        this.movieSeasonsService = movieSeasonsService;
        this.movieEpisodesService = movieEpisodesService;
    }

    @GetMapping("/{id}")
    public Movies getById(@PathVariable Long id) {
        return moviesService.getById(id);
    }
    @GetMapping("/listPage")
    public PageResponse<Movies> listPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(defaultValue = "0") int type,
            @RequestParam String name
    ) {
        QueryWrapper<Movies> queryWrapper = new QueryWrapper<>();
        if (type > 0) {
            queryWrapper.eq("type", type);
        }
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("title", name);
        }
        queryWrapper.orderByDesc("update_time");
        return PageConverter.convert(moviesService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @PostMapping
    public boolean save(@RequestBody Movies movieDetail) {
        return moviesService.save(movieDetail);
    }

    @PutMapping
    public boolean update(@RequestBody Movies movieDetail) {
        return moviesService.updateById(movieDetail);
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        return moviesService.removeById(id);
    }

    @GetMapping("/seasons/{movieId}")
    public List<MovieSeasons> getSeasonsByMovieId(@PathVariable Long movieId) {
        if (movieId == null) {
            return new ArrayList<>();
        }
        QueryWrapper<MovieSeasons> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("movie_id", movieId);
        queryWrapper.orderByAsc("season_number");
        return movieSeasonsService.list(queryWrapper);
    }
    @GetMapping("/episodesOnly/{id}")
    public MovieEpisodes getEpisodesById(@PathVariable Long id) {
        if (id == null) {
            return new MovieEpisodes();
        }
       return movieEpisodesService.getById(id);
    }
    @GetMapping("/episodes/{seasonsId}")
    public List<MovieEpisodes> getEpisodesBySeasonsId(@PathVariable Long seasonsId) {
        if (seasonsId == null) {
            return new ArrayList<>();
        }
        QueryWrapper<MovieEpisodes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("season_id", seasonsId);
        queryWrapper.orderByAsc("episode_number");
        return movieEpisodesService.list(queryWrapper);
    }
    @PostMapping("/seasons")
    public boolean saveSeasons(@RequestBody MovieSeasons movieSeasons) {
        return movieSeasonsService.save(movieSeasons);
    }
    @PostMapping("/episodes")
    public boolean saveEpisodes(@RequestBody MovieEpisodes movieEpisodes) {
        return movieEpisodesService.save(movieEpisodes);
    }
    @PutMapping("/seasons")
    public boolean updateSeasons(@RequestBody MovieSeasons movieSeasons) {
        return movieSeasonsService.updateById(movieSeasons);
    }
    @PutMapping("/episodes")
    public boolean updateEpisodes(@RequestBody MovieEpisodes movieEpisodes) {
        return movieEpisodesService.updateById(movieEpisodes);
    }
    @DeleteMapping("/seasons/{id}")
    public boolean deleteSeasons(@PathVariable Long id) {
        return movieSeasonsService.removeById(id);
    }
    @DeleteMapping("/episodes/{id}")
    public boolean deleteEpisodes(@PathVariable Long id) {
        return movieEpisodesService.removeById(id);
    }
}